//
//  DiTuViewController.m
//  LOU
//
//  Created by 要帅气的生活 on 15/11/10.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "DiTuViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "Head.h"
@interface DiTuViewController ()<MAMapViewDelegate,AMapSearchDelegate>

@property (nonatomic,retain) MAMapView *mapView;
@property (nonatomic,retain) AMapLocationManager *locationManager;
@property (nonatomic,retain) AMapSearchAPI *search;
@end

@implementation DiTuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitle:@"楼盘" midTitle:@"地图"];
    self.navigationItem.title = @"地图";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(camera)];
    
    [MAMapServices sharedServices].apiKey = @"60bbefa353ac21b9bab4113d8fd126c0";
    [AMapLocationServices sharedServices].apiKey = @"60bbefa353ac21b9bab4113d8fd126c0";
    [AMapSearchServices sharedServices].apiKey = @"60bbefa353ac21b9bab4113d8fd126c0";
    
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    _mapView.centerCoordinate = _mapView.userLocation.location.coordinate;
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(30.276951, 120.125042)];
    //CGPoint dizi = CGPointMake(30.276951, 120.125042);
    //[_mapView setZoomLevel:18 atPivot:dizi animated:YES];
    [_mapView setZoomLevel:18];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(30.276951, 120.125042);
    pointAnnotation.title = @"华星时代广场A座";
    pointAnnotation.subtitle = @"文3路106号";
    [_mapView addAnnotation:pointAnnotation];

//    _mapView.showsUserLocation = YES;//YES 为打开定位，NO为关闭定位
//    MAUserTrackingModeNone：不跟随用户位置，仅在地图上显示。
//    MAUserTrackingModeFollow：跟随用户位置移动，并将定位点设置成地图中心点。
//    MAUserTrackingModeFollowWithHeading：跟随用户的位置和角度移动。
    //[_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
        annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
        //annotationView.draggable = YES;        //设置标注可以拖动，默认为NO
        annotationView.pinColor = MAPinAnnotationColorPurple;
        return annotationView;
    }
    return nil;
}
//定位
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}
-(void)camera{
    CGRect inRect = CGRectMake(0, 64, WindowWidth, WindowHeight-128);
    UIImage *screenshotImage = [_mapView takeSnapshotInRect:inRect] ;
    UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"截图成功~" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
    [alert show];
}
@end
